package com.ub.party.web.controller;

import com.ub.party.common.constants.RespCodeEnum;
import com.ub.party.common.vo.resp.RespVo;
import com.ub.party.internal.model.MemberInfo;
import com.ub.party.internal.service.ShreecService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/shreec/")
public class ShreecController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(ShreecController.class);

  @Autowired
  private ShreecService shreecService;

  @RequestMapping(value = "register", method = RequestMethod.GET)
  public String register(Model model, HttpServletRequest request) {

    model.addAttribute("title", applicationPropertyConfig.getTitle());
    model.addAttribute("homePath", applicationPropertyConfig.getHomePath());
    return "memberRregister";
  }

  @RequestMapping(value = "members", method = {RequestMethod.POST})
  public String memberResult(Model model, @RequestParam(value = "company") String company, @RequestParam(value = "name") String name,
                             @RequestParam(value = "title") String title, @RequestParam(value = "cellphone") String cellphone,
                             @RequestParam(value = "wechat") String wechat, @RequestParam(value = "email") String email, HttpServletRequest request) {
    MemberInfo memberInfo = buildMemberInfo(company, name, title, cellphone, wechat, email);
    String ipAddress = getRemortIP(request);
    memberInfo.setIpAddress(ipAddress);
    try {
      RespVo respVo = shreecService.saveMember(memberInfo);
      if (RespCodeEnum.SUCCESS.getCode() == respVo.getCode()) {
        model.addAttribute("subTitle", applicationPropertyConfig.getSuccessSubTitle());
        model.addAttribute("description", applicationPropertyConfig.getSuccessDescription());
      } else {
        model.addAttribute("subTitle", applicationPropertyConfig.getFailureSubTitle());
        model.addAttribute("description", applicationPropertyConfig.getFailureDescription());
      }
    } catch (Exception e) {
      logger.error("", e);
      model.addAttribute("subTitle", applicationPropertyConfig.getFailureSubTitle());
      model.addAttribute("description", applicationPropertyConfig.getFailureDescription());
    }
    model.addAttribute("title", applicationPropertyConfig.getTitle());
    model.addAttribute("homePath", applicationPropertyConfig.getHomePath());
    return "memberResult";
  }

  private MemberInfo buildMemberInfo(String company, String name, String title, String cellphone, String wechat, String email) {
    MemberInfo memberInfo = new MemberInfo();
    memberInfo.setCompany(company);
    memberInfo.setName(name);
    memberInfo.setTitle(title);
    memberInfo.setCellphone(cellphone);
    memberInfo.setWechat(wechat);
    memberInfo.setEmail(email);
    return memberInfo;
  }
}


